package com.shell.customer.base.rx;

import com.shell.bee.base.utils.Encryptor;
import com.shell.bee.base.utils.RandomUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.base.utils.SystemUtil.SystemMode;
import com.shell.bee.sequence.SequenceHelper;
import com.shell.customer.base.CusEvent;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.feign.PubDictionaryFeign;
import com.shell.feign.PubRoleFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.governor.authorization.po.PubRole;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.organization.vo.PubOrganVo;
import com.shell.governor.user.vo.PubUserVo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SetCusUserAndOrganSubscriber extends OnErrorNotImplementedSubscriber<CusEvent> {
	@Autowired
	private PubDictionaryFeign pubDictionaryFeign;
	@Autowired
	private PubRoleFeign pubRoleFeign;

	public void onApplicationEvent(CusEvent event) {
		CusRecVo cusVo = event.getCusRecVo();

		PubOrganVo organVo = setOrgan(cusVo);

		PubUserVo userVo = setUser(cusVo, organVo.getOrganId(), event.isAdd());

		event.setOrgan(organVo);
		event.setUser(userVo);
	}

	private PubUserVo setUser(CusRecVo cusRecVo, String entId, boolean isAdd) {
		PubUserVo user = new PubUserVo();
		user.setUserName(cusRecVo.getContactorName());
		user.setPhoneNo(cusRecVo.getContactorPhone());
		user.setCertNo(cusRecVo.getCertNo());
		user.setCertType(cusRecVo.getCertType());
		if ("D".equals(cusRecVo.getCusState())) {
			user.setUserState("D");
		} else {
			user.setUserState("E");
		}
		user.setOrganId(entId);
		user.setEntId(entId);
		if (StringUtil.emptyAndNull(cusRecVo.getContactorId())) {
			List<PubRole> roleList = new ArrayList();

			PubDictionary dict = this.pubDictionaryFeign.getDictionaryByCode(cusRecVo.getCusType());
			String roleStr = dict.getExtension2();
			Set<String> roles = new HashSet();
			if (StringUtil.notTrimEmpty(roleStr)) {
				roles.addAll(Arrays.asList(roleStr.split(",")));
			}
			PubDictionary operatorDict;
			if (cusRecVo.isOperator()) {
				operatorDict = this.pubDictionaryFeign.getDictionaryByCode("OPERATOR");
				String operatorRoleStr = operatorDict.getExtension2();
				if (StringUtil.notTrimEmpty(operatorRoleStr)) {
					roles.addAll(Arrays.asList(operatorRoleStr.split(",")));
				}
			}
			if ((roles != null) && (roles.size() > 0)) {
				for (String code : roles) {
					PubRole role = new PubRole();
					role.setRoleCode(code);
					role.setRoleState("E");
					role = this.pubRoleFeign.selectOne(role);
					if (role != null) {
						roleList.add(role);
					}
				}
			}
			user.setRoles(roleList);

			user.setUserId(SystemUtil.getUUID());

			String inviteCode = cusRecVo.getCusCode() + SequenceHelper
					.generateCode(new StringBuilder().append("IC-").append(cusRecVo.getCusCode()).toString(), 2);
			user.setInviteCode(inviteCode);
		} else {
			user.setUserId(cusRecVo.getContactorId());
		}
		if (isAdd) {
			String password;
			if (SystemUtil.SystemMode.prod.equals(SystemUtil.getSystemMode())) {
				password = String.valueOf(RandomUtil.getRandomInt(8));
			} else {
				password = SpringContextUtil.getProperty("user.default.password");
			}
			user.setUnencryptedPassword(password);
			user.setPassword(Encryptor.encryptWithMD5(password));
		}
		return user;
	}

	private PubOrganVo setOrgan(CusRecVo cusRecVo) {
		PubOrganVo organVo = new PubOrganVo();
		if (StringUtil.emptyAndNull(cusRecVo.getCusId())) {
			organVo.setOrganId(SystemUtil.getUUID());
		} else {
			organVo.setOrganId(cusRecVo.getCusId());
		}
		organVo.setOrganCode(cusRecVo.getCusCode());
		organVo.setOrganName(cusRecVo.getCusName());
		organVo.setShortName(cusRecVo.getCusName());
		if ("D".equals(cusRecVo.getCusState())) {
			organVo.setOrganState("D");
		} else {
			organVo.setOrganState("E");
		}
		organVo.setOrganType("ENT");
		organVo.setSourceCode("EXTERNAL");

		PubDictionary dict = this.pubDictionaryFeign.getDictionaryByCode(cusRecVo.getCusType());
		if (StringUtil.notTrimEmpty(dict.getExtension1())) {
			organVo.setParentCode(dict.getExtension1());
		} else {
			organVo.setParentCode("OG001");
		}
		return organVo;
	}
}